package n1318;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class Main {

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
				
		HashMap<String, List<String>> map=new HashMap<String, List<String>>();
		
		String line=null;
		
		while((line=bf.readLine())!=null)
		{
			if(line.equals("XXXXXX"))break;
			char s[]=line.toCharArray();
			Arrays.sort(s);
			String t=String.valueOf(s);
			if(map.containsKey(t))
			{
				List<String> list=map.get(t);
				list.add(line);
				Collections.sort(list);
			}else {
				List<String> list=new ArrayList<String>();
				list.add(line);
				map.put(t, list);
			}
		}

		while((line=bf.readLine())!=null)
		{
			if(line.equals("XXXXXX"))break;
			char s[]=line.toCharArray();
			Arrays.sort(s);
			String t=String.valueOf(s);
			if(map.containsKey(t))
			{
				for(String v:map.get(t))
				{
					System.out.println(v);
				}
			}else {
				System.out.println("NOT A VALID WORD");
			}
			System.out.println("******");
		}
		
	}

}
